package com.hz.ys_system.dao;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.MyInterFace.SystemInterFace;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.SqlParameter;

/**
 * *****************************************************************************
 *               Copyright(c) 2010 by 哈尔滨华泽数码科技有限公司.
 *                       All rights reserved.
 *******************************************************************************
 *     File Name         :  com.hz.ys_system.dao.QueryDao.java
 *     Description(说明)	:  多条件查询
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description	  
 *     1		Jun 8, 2011				韩飞	              Created
 *****************************************************************************
 */
public class QueryDao implements SystemInterFace{
	
	/**
	 * @author 韩飞
	 * @time Mar 13, 2012	
	 * @param request
	 * @param sqlParams	传条件字段名
	 * @param sign		传与值之间的链接符号
	 * @return 
	 * 方法功能: 条件查询，
	 */
	public String QueryInfo(HttpServletRequest request,SqlParameter sqlParams,String sign){
		if(sign == null || "".equals(sign)){
			sign = "=";
		}
		StringBuilder sbSQL = new StringBuilder();
		String value, req ;
		if (sqlParams != null && !sqlParams.isEmpty()) {
			for(Object obj:sqlParams){
				if(obj.toString().indexOf(".")!=-1){
					req = obj.toString().split("\\.")[1];
				}else{
					req = obj.toString();
				}
				value = request.getParameter(req);
				if(value!=null && !"".equals(value)){
					sbSQL.append(" and ");
					sbSQL.append( obj.toString() + " "+sign+" ");
					if("like".equals(sign)){
						sbSQL.append(" '%"+value+"%' ");
					}else{
						sbSQL.append(" '"+value+"' ");
					}
					
					request.setAttribute(req , value);
				}
			}
		}
		return sbSQL.toString();
	}
	/**
	 * @author 韩飞
	 * @time Mar 28, 2012	
	 * @param request
	 * @param field  时间字段
	 * @param beginfield  开始
	 * @param endfield  结束
	 * @param where  都为空时是否要这个条件 true 为要  false 为不要
	 * @return 
	 * 方法功能: 时间字段的区间查询  默认都是查询当年的
	 */
	public String QueryDateInfo(HttpServletRequest request,String field,String beginfield,String endfield,boolean where){
		StringBuilder sbSQL = new StringBuilder();
		String begin = CommonDao.isnull(request.getParameter(beginfield)); //开始时间
		String end = CommonDao.isnull(request.getParameter(endfield)); //结束时间
		request.setAttribute(beginfield, begin);
		request.setAttribute(endfield, end);
		//时间字段
		sbSQL.append(" and "+field);
		if(!"".equals(begin)&&!"".equals(end)){
			sbSQL.append(" between '"+begin+"' and '"+end+"'");
		
		}else if(!"".equals(begin)){
			sbSQL.append(" between '"+begin+"' and '5000'");
		
		}else if(!"".equals(end)){
			sbSQL.append(" between '0' and '"+end+"'");
		
		}else{
			sbSQL.append(" between '"+DateUtil.getYear()+"-01-01' and '"+DateUtil.getTime()+"'");
		}
		if(!where && "".equals(begin)&& "".equals(end)){
			return "";
		}else {
			return sbSQL.toString();
		}
		
	}
}
